<template>
  <el-dialog
    :visible.sync="visible"
    :title="!dataForm.id ? $t('add') : $t('update')"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmitHandle()"
      :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'"
    >
      <el-form-item label="链接" prop="linkUrl">
        <el-input v-model="dataForm.linkUrl" placeholder="链接url"></el-input>
      </el-form-item>
      <el-form-item label="收益率" prop="hourIncome">
        <el-input
          v-model="dataForm.hourIncome"
          placeholder="每小时收益"
        ></el-input>
      </el-form-item>
      <el-form-item label="link价格" prop="urlAmt">
        <el-input v-model="dataForm.urlAmt" placeholder="url价格"></el-input>
      </el-form-item>
      <el-form-item prop="runType" :label="$t('product.runType')" size="mini">
        <el-radio-group v-model="dataForm.runType">
          <el-radio :label="'1'">{{ $t("product.runType1") }}</el-radio>
          <el-radio :label="'2'">{{ $t("product.runType2") }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item
        prop="resourceType"
        :label="$t('product.resourceType')"
        size="mini"
      >
        <el-radio-group v-model="dataForm.resourceType">
          <el-radio :label="'1'">{{ $t("product.resourceType1") }}</el-radio>
          <el-radio :label="'2'">{{ $t("product.resourceType2") }}</el-radio>
          <el-radio :label="'3'">{{ $t("product.resourceType3") }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="尺寸" prop="size">
        <el-input v-model="dataForm.size" placeholder="尺寸"></el-input>
      </el-form-item>
      <el-form-item label="图片列表" prop="img">
        <el-input type="textarea" v-model="dataForm.img" placeholder="请输入图片链接，如果有多张图片用英文的逗号:','隔开"></el-input>
      </el-form-item>
      <el-form-item prop="time" :label="$t('product.time')" size="mini">
        <el-radio-group v-model="dataForm.time">
          <el-radio :label="3">{{ $t("product.time1") }}</el-radio>
          <el-radio :label="5">{{ $t("product.time2") }}</el-radio>
          <el-radio :label="10">{{ $t("product.time3") }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item prop="adFilter" :label="$t('product.adFilter')" size="mini">
        <el-radio-group v-model="dataForm.adFilter">
          <el-radio :label="'1'">{{ $t("product.adFilter1") }}</el-radio>
          <el-radio :label="'2'">{{ $t("product.adFilter2") }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item prop="autoType" :label="$t('product.autoType')" size="mini">
        <el-radio-group v-model="dataForm.autoType">
          <el-radio :label="'1'">{{ $t("product.autoType1") }}</el-radio>
          <el-radio :label="'2'">{{ $t("product.autoType2") }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item
        label="IP地址"
        prop="ipAddress"
        v-if="dataForm.autoType == 2"
      >
        <el-input v-model="dataForm.ipAddress" placeholder="IP地址"></el-input>
      </el-form-item>
      <el-form-item
        label="子网掩码"
        prop="ipUAddress"
        v-if="dataForm.autoType == 2"
      >
        <el-input
          v-model="dataForm.ipUAddress"
          placeholder="子网掩码"
        ></el-input>
      </el-form-item>
      <el-form-item
        label="默认网关"
        prop="ipDAddress"
        v-if="dataForm.autoType == 2"
      >
        <el-input
          v-model="dataForm.ipDAddress"
          placeholder="默认网关"
        ></el-input>
      </el-form-item>
      <el-form-item prop="dnsType" :label="$t('product.dnsType')" size="mini">
        <el-radio-group v-model="dataForm.dnsType">
          <el-radio :label="'1'">{{ $t("product.dnsType1") }}</el-radio>
          <el-radio :label="'2'">{{ $t("product.dnsType2") }}</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="首选DNS" prop="dnsP" v-if="dataForm.dnsType == 2">
        <el-input
          v-model="dataForm.dnsP"
          placeholder="首选DNS服务器地址"
        ></el-input>
      </el-form-item>
      <el-form-item label="备用DNS" prop="dnsA" v-if="dataForm.dnsType == 2">
        <el-input
          v-model="dataForm.dnsA"
          placeholder="备用DNS服务器地址"
        ></el-input>
      </el-form-item>
    </el-form>

    <template slot="footer">
      <el-button @click="visible = false">{{ $t("cancel") }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{
        $t("confirm")
      }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from "lodash/debounce";
export default {
  data() {
    return {
      visible: false,
      fileList: [],
      baseUrl: window.SITE_CONFIG['apiURL'],
      src: [],
      dataForm: {
        id: "",
        linkUrl: "",
        hourIncome: "",
        urlAmt: "",
        createTime: 1,
        createBy: "",
        runType: "1",
        resourceType: "1",
        size: "",
        time: 5,
        adFilter: "1",
        autoType: "1",
        dnsType: "1",
        ipAddress: "",
        ipUAddress: "",
        ipDAddress: "",
        dnsP: "",
        dnsA: "",
        img: "",
      },
    };
  },
  computed: {
    dataRule() {
      return {
        linkUrl: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        hourIncome: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        urlAmt: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        createTime: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        createBy: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
      };
    },
  },
  methods: {
    init() {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();
        if (this.dataForm.id) {
          this.getInfo();
        }
      });
    },
    // 获取信息
    getInfo() {
      this.$http
        .get(`/hz/product/${this.dataForm.id}`)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.dataForm = {
            ...this.dataForm,
            ...res.data,
          };
        })
        .catch(() => {});
    },
    // 表单提交
    dataFormSubmitHandle: debounce(
      function() {
        this.$refs["dataForm"].validate((valid) => {
          if (!valid) {
            return false;
          }
          this.$http[!this.dataForm.id ? "post" : "put"](
            "/hz/product/",
            this.dataForm
          )
            .then(({ data: res }) => {
              if (res.code !== 0) {
                return this.$message.error(res.msg);
              }
              this.$message({
                message: this.$t("prompt.success"),
                type: "success",
                duration: 500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            })
            .catch(() => {});
        });
      },
      1000,
      { leading: true, trailing: false }
    ),
  },
};
</script>
